import openai
import os

from rich.console import Console
from rich.markdown import Markdown

messages = [
    {"role": "system", "content": "What can you do as an AI."},
]

console = Console()


def Inquiry():
    console.print("[bold green] You: \n[/bold green]")
    message = input()
    if message == "1":
        os.kill(pid, signal.SIGSTOP)
    if message:
        messages.append(
            {"role": "user", "content": message},
        )
        chat = openai.ChatCompletion.create(
            model="gpt-3.5-turbo", messages=messages
        )
    reply = chat.choices[0].message.content
    console.print("[bold red]\n ChatGPT: \n[/bold red]")
    console.print(Markdown(reply))
    print("\n")
    messages.append({"role": "assistant", "content": reply})


with open(mode="r+", file="./openai.api_key", encoding="utf-8") as f:
    key = f.read()
    console.print("[yellow]\n-------------------------------成功读取---------------------------------[/yellow]")
    print("openai_api_key :" + key)
    if key != "":
        openai.api_key = key
    else:
        openai.api_key = input("请输入openai.api_key")

if openai.api_key == "":
    print("openai.api_key为空 结束程序。")

console.print("[yellow]------------------------------------------------------------------------[/yellow]")
is_proxy = input("请输入代理地址[host]:[port]（无代理请敲回车忽略）：")

if is_proxy != "":
    os.environ["http_proxy"] = is_proxy
    os.environ["https_proxy"] = is_proxy
    console.print("[yellow]-------------------------------对话开始 (结束对话输入1)-----------------\n[/yellow]")
    while True:
        Inquiry()

else:
    print("没代理 还想玩chatgpt？那就试试吧 \n ")
    while True:
        Inquiry()
